



const { ccclass, property } = cc._decorator;

@ccclass
export default class Card_Alpha extends cc.Component {
    @property(cc.Label)
    rewardCoinLab: cc.Label = null;
    // 正面
    @property(cc.Node)
    Front: cc.Node = null;
    // 花色
    @property(cc.Sprite)
    Pattern: cc.Sprite = null;
    // 点数
    @property(cc.Sprite)
    Figure: cc.Sprite = null;
    // 花牌的人物
    @property(cc.Sprite)
    Face: cc.Sprite = null;
    // 鬼牌
    @property(cc.Sprite)
    WildCard: cc.Sprite = null;
    // 背面
    @property(cc.Sprite)
    Back: cc.Sprite = null;
    // 摸牌遮罩
    @property(cc.Node)
    SelectMask: cc.Node = null;
    // 高亮框
    @property(cc.Node)
    LightBorder: cc.Node = null;
    // 半透明遮罩
    @property(cc.Node)
    public TranslucentMask: cc.Node = null;

    // 花色贴图
    @property([cc.SpriteFrame])
    public Frame_Patterns: cc.SpriteFrame[] = [];
    // 点数贴图
    @property([cc.SpriteFrame])
    public Frame_Figures: cc.SpriteFrame[] = [];
    // 右下角形象贴图
    @property([cc.SpriteFrame])
    public Frame_Faces: cc.SpriteFrame[] = [];
    // 牌背贴图
    @property([cc.SpriteFrame])
    public Frame_Back: cc.SpriteFrame[] = [];

    // 扑克的正反面
    private _open: boolean = false;
    private _idx16: number = 0;
    private _pattern: number = 0;
    private _figure: number = 0;

    private _wildCard: number = 0;

    private _isClick: boolean = false;
    private _is4Color: boolean = false;


    private _matchType: number = -1;
    private _matchIndex: number = -1;
    private _matchRow: number = -1;
    private _matchCol: number = -1;
    private _location: cc.Vec2 = new cc.Vec2();
    private _touchOffset: cc.Vec2 = new cc.Vec2();
    onDestroy() {
        this.unscheduleAllCallbacks();
    }

    //----------------------------------------------------------------
    // getter、setter
    // _open
    public get open(): boolean {
        return this._open;
    }
    public set open(isOpen: boolean) {
        this._open = isOpen;
        this._initSide();
    }
    // _pattern
    public get pattern() {
        return this._pattern;
    }
    // _figure
    public get figure() {
        return this._figure;
    }
    // _idx16
    public get idx16() {
        return this._idx16;
    }

    public get box(): cc.Rect {
        return this.node.getBoundingBoxToWorld();
    }

    public get pos(): cc.Vec2 {
        return this.node.getPosition();
    }

    public set pos(_pos: cc.Vec2) {
        this.node.setPosition(_pos);
        let __pos = this.node.parent.convertToWorldSpaceAR(_pos);
        // this.node.zIndex = Math.floor(__pos.x) + CARD_ZINDEX;
        // if (__pos.y <= SCALE_LINE_DOWN) {
        //     this.node.scale != 1 && (this.node.scale = 1);
        // }
        // if (__pos.y >= SCALE_LINE_UP) {
        //     this.node.scale != SCALE_DESK && (this.node.scale = SCALE_DESK);
        // }
        // if (__pos.y > SCALE_LINE_DOWN && __pos.y < SCALE_LINE_UP) {
        //     let scale = 1 - (((__pos.y - SCALE_LINE_DOWN) / (SCALE_LINE_UP - SCALE_LINE_DOWN)) * (1 - SCALE_DESK));
        //     this.node.scale != scale && (this.node.scale = scale);
        // }
    }

    public get Clicked(): boolean {
        return this._isClick;
    }

    //----------------------------------------------------------------
    // method
    private _getCardColor(pattern: number) {
        // if (this._is4Color) {
        //     // 黑桃
        //     if (pattern == GameEnumShell.Pattern.Spade) {
        //         return cc.color(0, 0, 0);
        //     }
        //     // 红桃
        //     if (pattern == GameEnumShell.Pattern.Heart) {
        //         return cc.color(218, 26, 26);
        //     }
        //     // 梅花
        //     if (pattern == GameEnumShell.Pattern.Club) {
        //         return cc.color(38, 142, 29);
        //     }
        //     // 方块
        //     if (pattern == GameEnumShell.Pattern.Diamond) {
        //         return cc.color(35, 127, 205);
        //     }
        // } else {
        //     // 红桃，方块
        //     if (pattern == GameEnumShell.Pattern.Heart || pattern == GameEnumShell.Pattern.Diamond) {
        //         return cc.color(218, 26, 26);
        //     }
        //     // 黑桃，梅花
        //     if (pattern == GameEnumShell.Pattern.Spade || pattern == GameEnumShell.Pattern.Club) {
        //         return cc.color(0, 0, 0);
        //     }
        // }
    }

    private _getPatternIndex(): number {
        if (this._is4Color) {
            if (this.pattern < 2) {
                return this.pattern + 4;
            } else {
                return this.pattern;
            }
        } else {
            return this.pattern;
        }
    }

    private _isBigJoker() {
        //return this.pattern == GameEnumShell.Pattern.BigJoker;
    }

    //----------------------------------------------------------------
    // init
    public init(idx16: number, open: boolean, wildCard: number = 0) {
        // PhoneSdk.log("init idx16",idx16);
        // this._open = open;
        // this._idx16 = idx16;
        // this._wildCard = wildCard;
        // this._pattern = GroupHelper_Alpha.getPattern(idx16);
        // this._figure = GroupHelper_Alpha.getFigure(idx16);

        let is4Color = JSON.parse(cc.sys.localStorage.getItem('Card_is4Color'));
        is4Color == null && (is4Color = false);
        this._is4Color = is4Color;

        this._init();
    }

    private _init() {
        this._initColor();
        this._initPattern();
        this._initFigure();
        this._initFace();
        this.refreshWildCard();
        this._initSide();
        this._initBack();
    }

    // 设置颜色
    private _initColor() {
        // if (this._isBigJoker()) return;

        // let color = this._getCardColor(this.pattern);
        // if (this.Pattern && this.Pattern.node && this.Pattern.node.isValid && this.Pattern.node.color)
        //     this.Pattern.node.color = color;
        // if (this.Figure && this.Figure.node && this.Figure.node.isValid && this.Figure.node.color)
        //     this.Figure.node.color = color;
    }

    // 设置花色
    private _initPattern() {
        // if (this._isBigJoker()) {
        //     this.Pattern.node.active = false;
        // } else {
        //     this.Pattern.node.active = true;
        //     this.Pattern.spriteFrame = this.Frame_Patterns[this._getPatternIndex()];
        // }
    }

    // 设置点数
    private _initFigure() {
        // if (this._isBigJoker()) {
        //     this.Figure.spriteFrame = this.Frame_Figures[13];
        // } else {
        //     this.Figure.spriteFrame = this.Frame_Figures[this.figure - 1];
        // }
    }

    // 设置右下图片
    private _initFace() {
        // if (this.figure > 10 && this.figure < 14) {
        //     this.Face.spriteFrame = this.Frame_Faces[this.figure - 11];
        // } else {
        //     if (this._isBigJoker()) {
        //         this.Face.spriteFrame = this.Frame_Patterns[6];
        //     } else {
        //         this.Face.spriteFrame = this.Frame_Patterns[this._getPatternIndex()];
        //     }
        // }
    }

    // 设置鬼牌图标
    public refreshWildCard() {
        //this.WildCard.node.active = GroupHelper_Alpha.checkWild(this.idx16, this._wildCard) && !this._isBigJoker() && GroupHelper_Alpha.isShowWildCard();
    }

    // 设置牌堆鬼牌的图标位置
    public setCloseDeckWildIcon() {
        this.WildCard.node.setPosition(cc.v2(60, 72));
        // this.WildCard.node.setRotation(0, 0, -90);
        this.WildCard.node.angle = -90;
    }

    // 设置正反
    private _initSide() {
        this.Front.active = this._open;
        this.Back.node.active = !this._open;
    }

    public flipCard(duration: number = 0.5, vector: cc.Vec3 = cc.v3(0, -180, 0), reverse: boolean = false, callBack: () => void = null) {
        if (duration == 0) {
            this.Front.active = !reverse;
            this.Back.node.active = reverse;
            this.Front.scaleX = -1;
            this.node.setRotation(vector.x, vector.y, vector.z);

            callBack && callBack();
        } else {
            let action = cc.sequence(
                cc.spawn(cc.scaleTo(duration / 2, this.node.scale + 0.2), cc.rotate3DTo(duration / 2, vector.div(2))),
                cc.callFunc(() => {
                    //在这里切换扑克纹理
                    this.Front.active = !reverse;
                    this.Back.node.active = reverse;
                    this.Front.scaleX = -1;
                }),
                cc.spawn(cc.scaleTo(duration / 2, this.node.scale), cc.rotate3DTo(duration / 2, vector)),
                cc.callFunc(() => {
                    if (callBack != null) {
                        callBack()
                    }
                })
            )

            this.node.runAction(action);
        }
    }

    public setCardFront(sp: cc.SpriteFrame) {
        const backSprite = this.Front.getComponent(cc.Sprite);
        backSprite.spriteFrame = sp;
    }
    public setCardBack(sp: cc.SpriteFrame) {
        const backSprite = this.Back.getComponent(cc.Sprite);
        backSprite.spriteFrame = sp;
    }



    // 设置牌背
    private _initBack() {
        const backSprite = this.Back.getComponent(cc.Sprite);
        const sceneName = cc.director.getScene().name;
        switch (sceneName) {
            case "DragonVSTigerShell":
                backSprite.spriteFrame = this.Frame_Back[1];
                break;
            case "Solitaire":
                backSprite.spriteFrame = this.Frame_Back[2];
                break;
            default:
                backSprite.spriteFrame = this.Frame_Back[0];
                break;
        }
    }

    //----------------------------------------------------------------
    // 扑克效果
    // 高亮框闪烁
    public showLightBorder(isActive: boolean) {
        this.LightBorder.active = isActive;
    }
    // 半透遮罩
    public showTranslucentMask(isActive: boolean) {
        this.TranslucentMask.active = isActive;
    }
    // 摸牌提示动画
    public drawTipAnimation() {
        if (this && this.node && this.node.isValid) {
            this.SelectMask.active = true;
            this.LightBorder.active = true;

            this.scheduleOnce(() => {
                if (this && this.node && this.node.isValid) {
                    this.SelectMask.active = false;
                    this.LightBorder.active = false;
                }
            }, 1);
        }
    }
    // 翻牌
    

    // 移牌
    public moveCard(duration: number = 0.5, pos: cc.Vec2 = cc.Vec2.ZERO, scaleX: number = 1, scaleY: number = 1, callBack: () => void = null) {
        let action = cc.sequence(
            cc.spawn(cc.moveTo(duration, pos), cc.scaleTo(duration, scaleX, scaleY)),
            cc.rotateTo(0.1, 0),
            cc.callFunc(() => {
                if (callBack != null) {
                    callBack()
                }
            })
        )
        this.node.runAction(action);
    }

    // 点击
    public onClick() {
        this._isClick = !this._isClick;
        this.node.y += this._isClick ? 30 : -30;
    }

    public toLeft() {
        //this.pos = this.pos.sub(cc.v2(CARD_SHOW_WIDTH, 0))
    }

    public toRight() {
        //this.pos = this.pos.add(cc.v2(CARD_SHOW_WIDTH, 0))
    }
    public setMatch(index: number, row: number, col: number, type: number) {
        this._matchIndex = index;
        this._matchRow = row;
        this._matchCol = col;
        this._matchType = type;
    }

    public get matchRow(): number {
        return this._matchRow;
    }
    public set matchRow(isOpen: number) {
        this._matchRow = isOpen;
    }

    public get matchCol(): number {
        return this._matchCol;
    }
    public set matchCol(isOpen: number) {
        this._matchCol = isOpen;
    }

    public get matchIndex(): number {
        return this._matchIndex;
    }
    public set matchIndex(isOpen: number) {
        this._matchIndex = isOpen;
    }
    public get matchType(): number {
        return this._matchType;
    }
    public set matchType(isOpen: number) {
        this._matchType = isOpen;
    }
    public isTouch(pointX: number, pointY: number): boolean {
        var rectX = this.node.x;
        var rectY = this.node.y;
        var rectW = this.node.width * this.node.scaleX;
        var rectH = this.node.height * this.node.scaleY;
        var pX = rectX - rectW * 0.5;
        var pY = rectY - rectH * 0.5;

        var p2X = pX + rectW;
        var p2Y = pY + rectH;
        // console.log(pointX , pointY , rectX , rectY , rectW , rectH);
        if (pointX > pX && pointX < p2X && pointY > pY && pointY < p2Y) {
            return true;
        }
        return false;
    }
    public setLocation() {
        this._location.x = this.node.x;
        this._location.y = this.node.y;
    }
    public moveLocation() {
        let pos = new cc.Vec3(this._location.x, this._location.y, this.node.position.z);
        let pos1 = this.GetBetweenPoint(this.node.position, pos, 0.5);
        cc.tween(this.node)
            .to(0.1, { position: pos1, scale: 0.7 + 0.05, eulerAngles: new cc.Vec3(this.node.eulerAngles.x, this.node.eulerAngles.y, 15) })
            .to(0.1, { position: pos, scale: 0.7, eulerAngles: new cc.Vec3(this.node.eulerAngles.x, this.node.eulerAngles.y, 0) })
            .start();
    }
    public setTouchStartOffset(x, y) {
        this._touchOffset.x = x;
        this._touchOffset.y = y;
    }
    public getTouchStartOffset(): cc.Vec2 {
        return this._touchOffset;
    }
    public GetBetweenPoint(start: cc.Vec3, end: cc.Vec3, percent: number = 0.5): cc.Vec3 {
        let normal = end.sub(start).normalizeSelf();
        let distance = cc.Vec3.distance(start, end);
        return normal.scale(new cc.Vec3((distance * percent), (distance * percent), 1)).add(start);
    }
}
